import useChatStore from '@/store/modules/chat';
import { computed } from 'vue';

export function useConversation() {
  const chatStore = useChatStore();
  const conversations = computed(() => {

    const conversations = chatStore.conversations

    const list = [...conversations].reverse();
    const resultList = []

    for (let index = 0; index < list.length; index += 2) {
      const question = list[index];
      const conversation = list[index + 1];
      const last = resultList[resultList.length - 1] || {};
      if (last.question?.context === question.context) {
        last.conversations.push(conversation)
      } else {
        resultList.push({
          question,
          conversations: conversation ? [conversation]: []
        })
      }
    }
    return resultList
  })



  return {
    conversations
  }
}